package com.malbbmsomanager.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@SuppressLint("NewApi") public class DM_BBM_List_Adapters {
	// Database Name
	private static final String DATABASE_NAME = "dminvoicemanagerbbm.db";
	// Master Tables BBM
	private String FDEBTOR_TABLE = "fDebtor";
	private String FITEMS_TABLE = "FItems";
	private String FDDBNOTE_TABLE = "fddbnote";
	private static final int DATABASE_VERSION = 4;
	private static final String TAG = null;
	private DatabaseHelper dbHelperCustomer;
	private SQLiteDatabase mDb;

	public static final String KEY_CUSID = "_id";
	public static final String KEY_CUSCODE = "DebCode";
	public static final String KEY_CUSNAME = "DebName";
	public static final String KEY_ITEMCODE = "ItemCode";
	public static final String KEY_ITEMNAME = "ItemName";
	public static final String KEY_ITEMSTATUS = "ItemStatus";
	public static final String KEY_ITEMSEARCHSUM = "itemsummary";
	public static final String KEY_BRAND = "BrandCode";
	public static final String KEY_PACKSIZE = "packing";
	public static final String KEY_AREACODE = "AreaName";
	public static final String KEY_FREEQTY = "FreeItems";
	
	
	public static final String KEY_REFNO = "RefNo";
	public static final String KEY_RECID = "RecordId";
	

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		private static final String TAG = null;

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
		}
	}

	public DM_BBM_List_Adapters(Context ctx) {
		this.mCtx = ctx;
	}

	public DM_BBM_List_Adapters open() throws SQLException {
		dbHelperCustomer = new DatabaseHelper(mCtx);
		mDb = dbHelperCustomer.getWritableDatabase();
		return this;
	}

	public void close() {
		if (dbHelperCustomer != null) {
			dbHelperCustomer.close();
		}
	}

	// *****CUSTOMER DETAILS*****
	public Cursor fetchAllCustommers() {
		Log.v(TAG, "DJ-Filter Customers");
		Cursor mCursor = mDb.query(FDEBTOR_TABLE, new String[] { KEY_CUSID,
				KEY_CUSCODE, KEY_CUSNAME, KEY_AREACODE}, null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// *****CUSTOMER DETAILS ORDER BY*****
		public Cursor fetchAllCustommersOrderBy() {
			Log.v(TAG, "DJ-Filter Customers");
			String orderByCus =  DM_BBM_List_Adapters.KEY_CUSNAME + " ASC";
			/*Cursor mCursor = mDb.query(FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME , KEY_AREACODE}, null, null, null, null, orderByCus , null);*/
			Cursor mCursor = mDb.query(FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME , KEY_AREACODE}, null, null, null, null, orderByCus);
			
			if (mCursor != null) {
				mCursor.moveToNext();
			}
			return mCursor;
		}

	public Cursor fetchAllCustommersByName(String inputText)
			throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME, KEY_AREACODE }, null, null, null, null, null);

		} else {
			mCursor = mDb.query(true, FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME, KEY_AREACODE }, KEY_CUSNAME + " like '%"
					+ inputText + "%'", null, null, null, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
	
	public Cursor fetchAllCustommersByNameOrderBy(String inputText)
			throws SQLException {
		Log.w(TAG, inputText);
		String orderByCus =  DM_BBM_List_Adapters.KEY_CUSNAME + " ASC";
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME , KEY_AREACODE}, null, null, null, null, orderByCus);

		} else {
			/*mCursor = mDb.query(true, FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME , KEY_AREACODE }, KEY_CUSNAME + " like '%"
					+ inputText + "%'", null, null, null, null, orderByCus , null);*/
			mCursor = mDb.query(true, FDEBTOR_TABLE, new String[] { KEY_CUSID,
					KEY_CUSCODE, KEY_CUSNAME , KEY_AREACODE }, KEY_CUSNAME + " like '%"
					+ inputText + "%'", null, null, null, orderByCus, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// ****ITEM DETAILS*****
	public Cursor fetchAllItems() {
		Log.v(TAG, "DJ-Filter Items");
		String Flag = "0";
	    Cursor mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
				KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND,KEY_PACKSIZE,KEY_ITEMSTATUS,KEY_FREEQTY }, KEY_ITEMSTATUS + "=" + Flag, null , null, null, null);
	    //COLUMN_BID + "=" + Bid
		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}
	// ****FETACH ITEMS WITH ORDER*****
	public Cursor fetchAllItemsByOrder() {
		Log.v(TAG, "DJ-Filter Items");
		/*Cursor mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
				KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND,KEY_PACKSIZE }, null, null, null, null, null);*/
		String Flag = "0";
		String orderBy =  DM_BBM_List_Adapters.KEY_BRAND + " ASC";
		Cursor mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
				KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND,KEY_PACKSIZE,KEY_ITEMSTATUS,KEY_FREEQTY}, KEY_ITEMSTATUS + "=" + Flag, null, null, null, orderBy , null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	public Cursor fetchAllItemsByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		String Flag = "0";
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND,KEY_PACKSIZE,KEY_ITEMSTATUS,KEY_FREEQTY }, KEY_ITEMSTATUS + "=" + Flag, null, null, null, null);

		} else {
			mCursor = mDb.query(true, FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND,KEY_PACKSIZE,KEY_ITEMSTATUS,KEY_FREEQTY }, KEY_ITEMSEARCHSUM + " like '%"
					+ inputText + "%'" + " AND " + KEY_ITEMSTATUS + "=" + Flag, null, null, null, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
	
	public Cursor fetchAllItemsByCode(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		String Flag = "0";
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND, KEY_PACKSIZE, KEY_ITEMSTATUS,KEY_FREEQTY }, KEY_ITEMSTATUS + "=" + Flag, null, null, null, null);

		} else {
			mCursor = mDb.query(true, FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND, KEY_PACKSIZE, KEY_ITEMSTATUS,KEY_FREEQTY }, KEY_ITEMCODE + " like '%"
					+ inputText + "%'" + " AND " + KEY_ITEMSTATUS + "=" + Flag, null, null, null, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
	
	
	public Cursor fetchAllItemsByNameByOrder(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		String Flag = "0";
		String orderBy =  DM_BBM_List_Adapters.KEY_BRAND + " ASC";
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND, KEY_PACKSIZE, KEY_ITEMSTATUS, KEY_FREEQTY}, KEY_ITEMSTATUS + "=" + Flag, null, null, orderBy, null);

		} else {
			mCursor = mDb.query(true, FITEMS_TABLE, new String[] { KEY_CUSID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_BRAND, KEY_PACKSIZE, KEY_ITEMSTATUS , KEY_FREEQTY}, KEY_ITEMSEARCHSUM + " like '%"
					+ inputText + "%'" + " AND " + KEY_ITEMSTATUS + "=" + Flag, null, null, null, orderBy, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
	
	//Get All Receipt Details
	// *****CUSTOMER DETAILS*****
		public Cursor fetchAllReceipts() {
			Log.v(TAG, "DJ-Filter Customers");
			Cursor mCursor = mDb.query(FDDBNOTE_TABLE, new String[] { KEY_CUSID, KEY_REFNO,
					KEY_RECID, KEY_CUSCODE }, null, null, null, null, null);

			if (mCursor != null) {
				mCursor.moveToNext();
			}
			return mCursor;
		}
		
		
		public Cursor fetchAllReceiptsOrder(String inputText) throws SQLException {
			Log.w(TAG, inputText);
			String orderBy =  DM_BBM_List_Adapters.KEY_REFNO + " ASC";
			Cursor mCursor = null;
			if (inputText == null || inputText.length() == 0) {
				mCursor = mDb.query(FDDBNOTE_TABLE, new String[] { KEY_CUSID, KEY_REFNO,
						KEY_RECID, KEY_CUSCODE }, null, null, null, orderBy, null);

			} else {
				mCursor = mDb.query(true, FDDBNOTE_TABLE, new String[] { KEY_CUSID, KEY_REFNO,
						KEY_RECID, KEY_CUSCODE }, KEY_REFNO + " like '%"
						+ inputText + "%'", null, null, null, orderBy, null);
			}
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
			return mCursor;

		}
}
